package kr.saeildamil.androidstudy_second.ani;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.ImageView;

public class AnimCustom extends Activity {
	ImageView mAnimTarget;
	Button mBtnAnim;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch23_animcustom);
		
		mAnimTarget = (ImageView) findViewById(R.id.animtarget);
	}
	
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.btnskew:
			mAnimTarget.startAnimation(new skewAnim());
			break;
		case R.id.btncamera:
			mAnimTarget.startAnimation(new CameraAnim());
			break;
		}
	}
	
	class skewAnim extends Animation{
	 @Override
	public void initialize(int width, int height, int parentWidth,
			int parentHeight) {
		super.initialize(width, height, parentWidth, parentHeight);
		setDuration(1000);
		setInterpolator(new LinearInterpolator());
	 }
	 
	 @Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			super.applyTransformation(interpolatedTime, t);
			Matrix matrix = t.getMatrix();
			matrix.setSkew(0.5f * interpolatedTime, 0);
	  }
	}
	
	class CameraAnim extends Animation{
		float cx, cy;
		@Override
		public void initialize(int width, int height, int parentWidth,
				int parentHeight) {
			super.initialize(width, height, parentWidth, parentHeight);
			cx = width / 2;
			cy = height / 2;
			setDuration(1000);
			setInterpolator(new LinearInterpolator());
		}
		
		@Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			super.applyTransformation(interpolatedTime, t);
			Camera cam = new Camera();
			cam.rotateY(360 * interpolatedTime);
			Matrix matrix = t.getMatrix();
			cam.getMatrix(matrix);
			matrix.preTranslate(-cx,  -cy);
			matrix.postTranslate(cx, cy);
		}
	}
}
